<template>
  <div class="course-create">
    <el-row style="margin-bottom: 20px" class="header">
      <el-page-header
      @back="$router.back()"
      :content="isEdit ? form.courseName : '添加课程'">
      </el-page-header>
    </el-row>
    <el-card>
      <div slot="header">
        <el-steps :active="activeStep" simple>
            <el-step
              v-for="(step, index) of steps"
              :key="index"
              :title="step.name"
              @click.native="activeStep = index"
              :icon="step.icon"></el-step>
        </el-steps>
      </div>
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <div v-show="activeStep === 0">
          <el-form-item label="名称" prop="courseName">
            <el-input
              type="text"
              placeholder="请输入名称"
              v-model="form.courseName"
              maxlength="60"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-form-item label="简介" prop="brief">
            <el-input
              type="text"
              placeholder="请输入简介"
              v-model="form.brief"
              maxlength="100"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-form-item label="讲师姓名" prop="teacherDTO.teacherName">
            <el-input
              type="text"
              placeholder="请输入讲师姓名"
              v-model="form.teacherDTO.teacherName"
              maxlength="100"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-form-item label="职位" prop="teacherDTO.position">
            <el-input
              type="text"
              placeholder="请输入讲师职位"
              v-model="form.teacherDTO.position"
              maxlength="50"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-form-item label="讲师简介" prop="teacherDTO.description">
            <el-input
              type="text"
              placeholder="请输入讲师简介"
              v-model="form.teacherDTO.description"
              maxlength="100"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-row class="flex">
            <el-form-item label="课程概述" prop="previewFirstField" style="width: 50%">
              <el-input
                type="text"
                placeholder="请输入课程概述"
                v-model="form.previewFirstField"
                maxlength="20"
                show-word-limit
              ></el-input>
            </el-form-item>
            <el-form-item prop="previewSecondField" style="width: 50%">
              <el-input
                type="text"
                placeholder="请输入课程概述"
                v-model="form.previewSecondField"
                maxlength="20"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-row>
          <el-form-item label="课程排序" prop="sortNum">
            <el-input placeholder="请输入课程排序" v-model="form.sortNum" type="number">
              <template slot="append">数字控制排序，数字越大越靠后</template>
            </el-input>
          </el-form-item>
        </div>
        <div v-show="activeStep === 1">
          <el-form-item label="课程封面" prop="courseListImg">
            <el-upload
              action="/boss/course/upload"
              list-type="picture-card"
              ref="courseListImg"
              :file-list="form.courseListImg ? [{}] : []"
              :on-exceed="onExceed"
              :limit="1"
              :on-success="courseListImgSuccess"
              :auto-upload="true">
                <i slot="default" class="el-icon-plus"></i>
                <div slot="tip">
                  <el-tag type="info">建议尺寸：230*300px，JPG、PNG格式，图片小于150K</el-tag>
                </div>
                <div slot="file" slot-scope="{file}">
                  <img
                    class="el-upload-list__item-thumbnail"
                    :src="form.courseListImg" alt=""
                  >
                  <span class="el-upload-list__item-actions">
                    <span
                      class="el-upload-list__item-delete"
                      @click="courseListImgRemove(file)"
                    >
                      <i class="el-icon-delete"></i>
                    </span>
                  </span>
                </div>
            </el-upload>
          </el-form-item>
          <el-form-item label="解锁封面" prop="courseImgUrl">
            <el-upload
              action="/boss/course/upload"
              list-type="picture-card"
              ref="courseImg"
              :file-list="form.courseImgUrl ? [{}] : []"
              :on-exceed="onExceed"
              :limit="1"
              :on-success="courseImgSuccess"
              :auto-upload="true">
                <i slot="default" class="el-icon-plus"></i>
                <div slot="tip">
                  <el-tag type="info">建议尺寸：230*300px，JPG、PNG格式，图片小于150K</el-tag>
                </div>
                <div slot="file" slot-scope="{file}">
                  <img
                    class="el-upload-list__item-thumbnail"
                    :src="form.courseImgUrl" alt=""
                  >
                  <span class="el-upload-list__item-actions">
                    <span
                      v-if="form.courseImgUrl"
                      class="el-upload-list__item-delete"
                      @click="courseImgRemove(file)"
                    >
                      <i class="el-icon-delete"></i>
                    </span>
                  </span>
                </div>
            </el-upload>
          </el-form-item>
        </div>
        <div v-show="activeStep === 2">
          <el-form-item label="售卖价格" prop="discounts">
            <el-input
              type="number"
              placeholder="请输入售价"
              v-model="form.discounts"
              show-word-limit
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="商品原价" prop="price">
            <el-input
              type="number"
              placeholder="请输入商品原价"
              v-model="form.price"
              show-word-limit
            >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="销量" prop="sales">
            <el-input
              type="number"
              placeholder="请输入销量"
              v-model="form.sales"
              show-word-limit
            >
              <template slot="append">单</template>
            </el-input>
          </el-form-item>
          <el-form-item label="活动标签" prop="discountsTag">
            <el-input
              type="text"
              placeholder="请输入活动标签"
              v-model="form.discountsTag"
              maxlength="4"
              show-word-limit
            ></el-input>
          </el-form-item>
        </div>
        <div v-show="activeStep === 3">
          <el-form-item label="限时秒杀开关" prop="activityCourse">
            <el-switch
              v-model="form.activityCourse"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <transition name="el-zoom-in-top">
            <el-row v-show="form.activityCourse && form.activityCourseDTO">
              <el-form-item label="开始时间" prop="activityCourseDTO.beginTime">
                <el-date-picker
                  v-model="form.activityCourseDTO.beginTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择开始时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="结束时间" prop="activityCourseDTO.endTime">
                <el-date-picker
                  v-model="form.activityCourseDTO.endTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择结束时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="秒杀价" prop="activityCourseDTO.amount">
                <el-input
                  type="number"
                  placeholder="请输入秒杀价"
                  v-model="form.activityCourseDTO.amount"
                  show-word-limit
                >
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
              <el-form-item label="秒杀库存" prop="activityCourseDTO.stock">
                <el-input
                  type="number"
                  placeholder="请输入秒杀库存"
                  v-model="form.activityCourseDTO.stock"
                  show-word-limit
                >
                  <template slot="append">个</template>
                </el-input>
              </el-form-item>
            </el-row>
          </transition>
        </div>
        <div v-show="activeStep === 4">
            课程详情
            <mark-down-editor :value="form.courseDescriptionMarkDown" ref="md" />
            <el-form-item style="text-align: center; margin-top: 20px">
              <el-button type="primary" @click="onSubmit">提交</el-button>
            </el-form-item>
        </div>
        <el-form-item style="text-align: center">
            <el-button type="primary" @click="activeStep++" v-if="activeStep >= 0 && activeStep < steps.length - 1">下一步</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { Form, Upload } from 'element-ui'
import Vue from 'vue'
import MarkDownEditor from './MarkDownEditor.vue'
import { saveOrUpdateCourse, getCourseById } from '@/services/course'

export default Vue.extend({
  name: 'Course',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    courseId: {
      type: [String, Number]
    }
  },
  created () {
    if (this.isEdit) {
      this.LoadCourse()
    }
  },
  components: {
    MarkDownEditor
  },
  data () {
    return {
      activeStep: 0,
      disabled: true,
      steps: [
        {
          name: '基本信息',
          icon: 'el-icon-edit'
        }, {
          name: '课程封面',
          icon: 'el-icon-upload'
        }, {
          name: '销售信息',
          icon: 'el-icon-picture'
        }, {
          name: '秒杀活动',
          icon: 'el-icon-upload'
        }, {
          name: '课程详情',
          icon: 'el-icon-edit'
        }
      ],
      form: {
        id: null,
        courseName: '',
        brief: '',
        teacherDTO: {
          id: null,
          courseId: null,
          teacherName: '',
          teacherHeadPicUrl: '',
          position: '',
          description: ''
        },
        courseDescriptionMarkDown: '',
        price: '',
        discounts: '',
        priceTag: '',
        discountsTag: '',
        isNew: true,
        isNewDes: '',
        courseListImg: '',
        courseImgUrl: '',
        sortNum: '',
        previewFirstField: '',
        previewSecondField: '',
        status: 0,
        sales: '',
        activityCourse: false,
        activityCourseDTO: {
          id: null,
          courseId: null,
          beginTime: '',
          endTime: '',
          amount: '',
          stock: ''
        },
        autoOnlineTime: ''
      },
      rules: {
        courseName: [
          { required: true, message: '请输入课程名称', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ],
        brief: [
          { required: true, message: '请输入简介', trigger: 'blur' },
          { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
        ],
        'teacherDTO.teacherName': [
          { required: true, message: '请输入讲师姓名', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        'teacherDTO.position': [
          { required: true, message: '请输入讲师职位', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        'teacherDTO.description': [
          { required: true, message: '请输入讲师简介', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ],
        previewFirstField: [
          { required: true, message: '请输入课程概述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        sortNum: [
          { required: true, message: '请输入课程排序', trigger: 'blur' }
        ],
        discounts: [
          { required: true, message: '请输入售卖价格', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    courseListImgRemove () {
      (this.$refs.courseListImg as Upload).clearFiles()
    },
    courseImgRemove () {
      (this.$refs.courseImg as Upload).clearFiles()
    },
    courseListImgSuccess (file: any) {
      this.form.courseListImg = file.data.name
    },
    courseImgSuccess (file: any) {
      this.form.courseImgUrl = file.data.name
    },
    onExceed () {
      this.$message.error('每个板块只能上传一张图片！请先移除图片再上传')
    },
    async onSubmit () {
      try {
        await (this.$refs.form as Form).validate()
      } catch (error) {
        return this.$message.error('请检查是否填写正确')
      }
      const value = (this.$refs.md as any).getValue()
      this.form.courseDescriptionMarkDown = value
      await saveOrUpdateCourse(this.form)
      this.$message.success('添加成功')
      this.$router.push({
        name: 'course'
      })
    },
    async LoadCourse () {
      const { data } = await getCourseById(this.courseId)
      console.log((this.$refs.md as any).setValue(data.data.courseDescriptionMarkDown))
      if (!data.data.activityCourseDTO) {
        data.data.activityCourseDTO = {
          id: null,
          courseId: null,
          beginTime: '',
          endTime: '',
          amount: '',
          stock: ''
        }
      }
      this.form = data.data
    }
  }
})
</script>

<style lang="scss" scoped>
.el-steps {
  cursor: pointer;
}
.flex {
  display: flex;
  justify-content: space-between;
}
.header {
  display: flex;
  margin-bottom: 20px;
}
</style>
